#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2005                 ##
##                                                                            ##
## This program is free software;  you can redistribute it and#or modify      ##
## it under the terms of the GNU General Public License as published by       ##
## the Free Software Foundation; either version 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program is distributed in the hope that it will be useful, but        ##
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
## for more details.                                                          ##
##                                                                            ##
## You should have received a copy of the GNU General Public License          ##
## along with this program;  if not, write to the Free Software               ##
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   icmp4-multi-diffnic01
#
# Description:
#   Verify that the kernel is not crashed with receiving and sending ICMP
#   message at differnt NIC with the following conditions
#     - The version of IP is IPv4
#     - IPsec is not used
#
#   *) This script may be read by the other test case
#
# Setup:
#   See testcases/network/stress/README
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# History:
#	Oct 19 2005 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
# Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

# The test case ID, the test case count and the total number of test case
TCID=${TCID:-icmp4-multi-diffnic01}
TST_TOTAL=1
TST_COUNT=1
export TCID
export TST_COUNT
export TST_TOTAL

# Test description
tst_resm TINFO "Verify that the kernel is not crashed with receiving and sending various size of ICMP message at differnt NIC simultaneously with the following conditions"

# Make sure the value of LTPROOT
LTPROOT=${LTPROOT:-`(cd ../../../../.. ; pwd)`}
export LTPROOT

# Check the environmanet variable
. check_envval || exit $TST_TOTAL

# Dulation of the test [sec]
NS_DURATION=${NS_DURATION:-3600}      # 1 hour

# The version of IP
IP_VER=${IP_VER:-4}

# true, if ipsec is used
DO_IPSEC=${DO_IPSEC:-false}

# The value of SPI
SPI=${SPI:-1000}

# IPsec Protocol ( ah / esp / ipcomp )
IPSEC_PROTO=${IPSEC_PROTO:-ah}

# IPsec Mode ( transport / tunnel )
IPSEC_MODE=${IPSEC_MODE:-transport}

# Array of the echo request packet size
ICMP_SIZE_ARRAY=${ICMP_SIZE_ARRAY:-"10 100 1000 10000 65507"}



#-----------------------------------------------------------------------
#
# Function: do_cleanup
#
# Description:
#   Recover the system configuration
#
#-----------------------------------------------------------------------
do_cleanup()
{
    # Kill the icmp traffic server
    killall_icmp_traffic

    # Unset SAD/SPD
    output_ipsec_conf flush | setkey -c >/dev/null 2>&1
    $LTP_RSH $RHOST ${LTPROOT}/'testcases/bin/output_ipsec_conf flush | PATH=/sbin:/usr/sbin:$PATH setkey -c' >/dev/null 2>&1

    # Clean up each interface
    link_num=0
    while [ $link_num -lt $link_total ]; do
	initialize_if lhost ${link_num}
	initialize_if rhost ${link_num}
	link_num=`expr $link_num + 1`
    done
}


#-----------------------------------------------------------------------
#
# Setup
#

# Unset the maximum number of processes
ulimit -u unlimited

# Output the informaion
tst_resm TINFO "- Test duration is $NS_DURATION [sec]"

link_total=`echo $LHOST_HWADDRS | wc -w`
rhost_link_total=`echo $RHOST_HWADDRS | wc -w`
if [ $link_total -ne $rhost_link_total ]; then
    tst_resm TBROK "The number of element in LHOST_HWADDRS differs from RHOST_HWADDRS"
    exit 1
fi
if [ $link_total -lt 2 ]; then
    tst_resm TBROK "This test case requires plural NICs."
    exit 1
fi
tst_resm TINFO "- Target number of the connection is $link_total"

tst_resm TINFO "- Version of IP is IPv${IP_VER}"
tst_resm TINFO "- Size of packets are ( $ICMP_SIZE_ARRAY )"

if $DO_IPSEC ; then
    message=`check_setkey`
    if [ $? -ne 0 ]; then
	tst_resm TBROK "$message"
	exit 1
    fi

    case $IPSEC_PROTO in
	ah)
	tst_resm TINFO "- IPsec [ AH / $IPSEC_MODE ]"
	;;
	esp)
	tst_resm TINFO "- IPsec [ ESP / $IPSEC_MODE ]"
	;;
	ipcomp)
	tst_resm TINFO "- IPcomp [ $IPSEC_MODE ]"
	;;
    esac
fi

# Initialize the system configuration
do_cleanup

# Call do_cleanup function before exit
trap do_cleanup 0

# Loop for NIC configuration
link_num=0
lhost_addrs=""
while [ $link_num -lt $link_total ]; do
    # name of interface of the local/remote host
    lhost_ifname=`get_ifname lhost $link_num`
    rhost_ifname=`get_ifname rhost $link_num`

    # Set the IP address to each interface
    case $IP_VER in
	4)
	network_part="10.0.${link_num}"
	network_mask=24
	lhost_host_part="2"     # local host
	rhost_host_part="1"     # remote host
	set_ipv4addr lhost $link_num $network_part $lhost_host_part
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "Failed to set IPv4 address at the local host"
	    exit 1
	fi
	set_ipv4addr rhost $link_num $network_part $rhost_host_part
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "Failed to set IPv4 address at the remote host"
	    exit 1
	fi

	# IPv4 address of the local/remote host
	lhost_addr="${network_part}.${lhost_host_part}"
	rhost_addr="${network_part}.${rhost_host_part}"
	lhost_addrs="${lhost_addrs} ${lhost_addr}"
	;;

	6)
	network_part="fd00:1:0:`printf %x ${link_num}`"
	network_mask=64
	lhost_host_part=":2"     # local host
	rhost_host_part=":1"     # remote host
	add_ipv6addr lhost $link_num $network_part $lhost_host_part
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "Failed to set IPv6 address at the local host"
	    exit 1
	fi
	add_ipv6addr rhost $link_num $network_part $rhost_host_part
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "Failed to set IPv6 address at the remote host"
	    exit 1
	fi
	lhost_addr="${network_part}:${lhost_host_part}"
	rhost_addr="${network_part}:${rhost_host_part}"
	lhost_addrs="${lhost_addrs} ${lhost_addr}"
	;;

	*)
	tst_resm TBROK "Unknown IP version"
	;;
    esac

    # Configure SAD/SPD
    if $DO_IPSEC ; then
	ipsec_log=`mktemp -p $TMPDIR`

	# Set SAD/SPD according to the variables
	output_ipsec_conf src \
	    $IPSEC_PROTO $IPSEC_MODE $SPI $lhost_addr $rhost_addr \
		| setkey -c 2>&1 | tee $ipsec_log
	if [ $? -ne 0 -o -s $ipsec_log ]; then
	    rm -f $ipsec_log
	    tst_resm TBROK "Failed to configure SAD/SPD on the local host."
	    exit 1
	fi

	$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/output_ipsec_conf dst $IPSEC_PROTO $IPSEC_MODE $SPI $lhost_addr $rhost_addr' | PATH=/sbin:/usr/sbin:$PATH setkey -c' 2>&1 | tee $ipsec_log
	if [ $? -ne 0 -o -s $ipsec_log ]; then
	    rm -f $ipsec_log
	    tst_resm TBROK "Failed to configure SAD/SPD on the remote host."
	    exit 1
	fi
	rm -f $ipsec_log
    fi

    # Make sure the connectivity
    case $IP_VER in
	4)
	ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv4_connectivity $rhost_ifname $lhost_addr' ; echo $?'`
	if [ $ret -ne 0 ]; then
	    tst_resm TBROK "There is no IPv4 connectivity on Link${link_num}"
	    exit 1
	fi
	;;

	6)
	ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv6_connectivity $rhost_ifname $lhost_addr' ; echo $?'`
	if [ $ret -ne 0 ]; then
	    tst_resm TBROK "There is no IPv6 connectivity on Link${link_num}"
	    exit 1
	fi
	;;
    esac

    link_num=`expr $link_num + 1`
done


#-----------------------------------------------------------------------
#
# Main
#
#

connection_num=0
while [ $connection_num -lt $link_total ]; do
    field=`expr $connection_num + 1`
    lhost_addr=`echo $lhost_addrs | cut -d ' ' -f $field`

    lhost_ifname=`get_ifname lhost $connection_num`
    rhost_ifname=`get_ifname rhost $connection_num`

    # Run a client
    $LTP_RSH $RHOST "${LTPROOT}/testcases/bin/ns-echoclient -S $lhost_addr -f $IP_VER -s \"$ICMP_SIZE_ARRAY\"" &
    connection_num=`expr $connection_num + 1`
done

sleep $NS_DURATION
killall_icmp_traffic
wait

#-----------------------------------------------------------------------
#
# Clean up
#

tst_resm TPASS "Test is finished successfully."
exit 0
